<template>
  <div class="app-container">
    <el-form
      :model="queryParams"
      ref="queryForm"
      :inline="true"
      v-show="showSearch"
      label-width="120px"
    >
      <el-form-item label="原编码" prop="cdCode">
        <el-input
          v-model="queryParams.cdCode"
          placeholder="请输入原编码"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="观测含水层层位" prop="gcSccw">
        <el-input
          v-model="queryParams.gcSccw"
          placeholder="请输入观测含水层层位"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="观测含水层层厚" prop="gcScch">
        <el-input
          v-model="queryParams.gcScch"
          placeholder="请输入观测含水层层厚"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="观测含水层埋深" prop="gcScms">
        <el-input
          v-model="queryParams.gcScms"
          placeholder="请输入观测含水层埋深"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="孔深" prop="gcKs">
        <el-input
          v-model="queryParams.gcKs"
          placeholder="请输入孔深"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="孔径" prop="gcKj">
        <el-input
          v-model="queryParams.gcKj"
          placeholder="请输入孔径"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['define:holeDefine:add']"
          >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['define:holeDefine:edit']"
          >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['define:holeDefine:remove']"
          >删除
        </el-button>
      </el-col>
      <right-toolbar
        :showSearch.sync="showSearch"
        @queryTable="getList"
      ></right-toolbar>
    </el-row>

    <el-table
      v-loading="loading"
      :data="holeDefineList"
      @selection-change="handleSelectionChange"
    >
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" width="80" align="center">
        <template slot-scope="scope">
          <span style="margin-left: 10px">{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="原编码" align="center" prop="cdCode"  width="150"/>
      <el-table-column label="测点编码" align="center" prop="remark"  width="150"/>
      <el-table-column label="传感器类型" align="center" prop="sensorType"  width="150"/>
      <el-table-column
        label="传感器安装位置"
        align="center"
        prop="sensorInstallation"
         width="150"
      />
      <el-table-column
        label="观测含水层层位"
        align="center"
        prop="gcSccw"
         width="150"
      />
      <el-table-column
        label="观测含水层层厚"
        align="center"
        prop="gcScch"
         width="150"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.gcScch }} <span class="price">m</span></span>
        </template>
      </el-table-column>
      <el-table-column
        label="观测含水层埋深"
        align="center"
        prop="gcScms"
         width="150"
      >
        <template slot-scope="scope">
          <span>{{ scope.row.gcScms }} <span class="price">m</span></span>
        </template>
      </el-table-column>
      <el-table-column label="孔深" align="center" prop="gcKs"  width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.gcKs }} <span class="price">m</span></span>
        </template>
      </el-table-column>
      <el-table-column label="孔径" align="center" prop="gcKj"  width="150">
        <template slot-scope="scope">
          <span>{{ scope.row.gcKs }} <span class="price">mm</span></span>
        </template>
      </el-table-column>
      <el-table-column label="孔口坐标X" align="center" prop="kkX"  width="150"/>
      <el-table-column label="孔口坐标Y" align="center" prop="kkY"  width="150"/>
      <el-table-column label="孔口坐标Z" align="center" prop="kkZ"  width="150"/>
      <el-table-column
        label="数据定义时间"
        align="center"
        prop="dataTime"
        width="180"
      >
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.dataTime, "{y}-{m}-{d}") }}</span>
        </template>
      </el-table-column>

      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
         width="150"
         fixed="right"
      >
        <template slot-scope="scope">
          <el-button-group>
            <el-button
              size="small"
              type="success"
              icon="el-icon-edit"
              @click="handleUpdate(scope.row)"
              v-hasPermi="['define:holeDefine:edit']"
            >
            </el-button>
            <el-button
              size="small"
              type="danger"
              icon="el-icon-delete"
              @click="handleDelete(scope.row)"
              v-hasPermi="['define:holeDefine:remove']"
            >
            </el-button>
          </el-button-group>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改长观孔测点基础信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="550px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="原编码" prop="cdCode">
          <el-input v-model="form.cdCode" placeholder="请输入原编码" />
        </el-form-item>
        <el-form-item label="传感器类型" prop="sensorType">
          <el-select
            v-model="form.sensorType"
            placeholder="请选择传感器类型"
            style="width:100%"
          >
            <el-option
              v-for="dict in sensorTypeList"
              :key="dict.sensorTypeCode"
              :label="dict.sensorTypeDesc"
              :value="dict.sensorTypeCode"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="传感器安装位置" prop="sensorInstallation">
          <el-input
            v-model="form.sensorInstallation"
            placeholder="请输入传感器安装位置"
          />
        </el-form-item>
        <el-form-item label="观测含水层层位" prop="gcSccw">
          <el-input v-model="form.gcSccw" placeholder="请输入观测含水层层位" />
        </el-form-item>
        <el-form-item label="观测含水层层厚" prop="gcScch">
          <el-input
            v-model="form.gcScch"
            placeholder="请输入观测含水层层厚"
            style="width:80%"
            @input="inputnum"
          />
          <span class="unit">m</span>
        </el-form-item>
        <el-form-item label="观测含水层埋深" prop="gcScms">
          <el-input
            v-model="form.gcScms"
            placeholder="请输入观测含水层埋深"
            style="width:80%"
            @input="inputnum"
          />
          <span class="unit">m</span>
        </el-form-item>
        <el-form-item label="孔深" prop="gcKs">
          <el-input
            v-model="form.gcKs"
            placeholder="请输入孔深"
            style="width:80%"
            @input="inputnum"
          />
          <span class="unit">m</span>
        </el-form-item>
        <el-form-item label="孔径" prop="gcKj">
          <el-input
            v-model="form.gcKj"
            placeholder="请输入孔径"
            style="width:80%"
            @input="inputnum"
          />
          <span class="unit">mm</span>
        </el-form-item>
        <el-form-item label="孔口坐标X" prop="kkX">
          <el-input v-model="form.kkX" placeholder="请输入孔口坐标X" />
        </el-form-item>
        <el-form-item label="孔口坐标Y" prop="kkY">
          <el-input v-model="form.kkY" placeholder="请输入孔口坐标Y" />
        </el-form-item>
        <el-form-item label="孔口坐标Z" prop="kkZ">
          <el-input v-model="form.kkZ" placeholder="请输入孔口坐标Z" />
        </el-form-item>
        <el-form-item label="数据定义时间" prop="dataTime">
          <el-date-picker
            clearable
            size="small"
            v-model="form.dataTime"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="选择数据定义时间"
            style="width: 100%;"
          >
          </el-date-picker>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  listHoleDefine,
  getHoleDefine,
  delHoleDefine,
  addHoleDefine,
  updateHoleDefine
} from "@/api/define/holeDefine";
import { listSensorTypeData } from "@/api/dictData/sensorTypeData";
export default {
  name: "HoleDefine",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 长观孔测点基础信息表格数据
      holeDefineList: [],
      //传感器类型
      sensorTypeList: [],
      TypeParams: {
        pageNum: 1,
        pageSize: 100
      },
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 数据定义时间时间范围
      daterangeDataTime: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        mineCode: null,

        mineName: null,

        cdCode: null,

        sensorType: null,

        sensorInstallation: null,

        gcSccw: null,

        gcScch: null,

        gcScms: null,

        gcKs: null,

        gcKj: null,

        kkX: null,

        kkY: null,

        kkZ: null,

        dataTime: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        cdCode: [
          { required: true, message: "原编码不能为空", trigger: "blur" },
          { min: 1, max: 8, message: "长度在 1 到 8 个字符", trigger: "blur" }
        ],

        sensorType: [
          { required: true, message: "传感器类型不能为空", trigger: "blur" }
        ],

        sensorInstallation: [
          { required: true, message: "传感器安装位置不能为空", trigger: "blur" }
        ],

        gcSccw: [
          { required: true, message: "观测含水层层位不能为空", trigger: "blur" }
        ],

        gcScch: [
          {
            required: true,
            message: "观测含水层层厚不能为空",
            trigger: "blur"
          },
          {
            min: 1,
            max: 8,
            message: "长度在 1 到 8 个字符,小数点后面2位",
            trigger: "blur"
          }
        ],

        gcScms: [
          {
            required: true,
            message: "观测含水层埋深不能为空",
            trigger: "blur"
          },
          {
            min: 1,
            max: 8,
            message: "长度在 1 到 8 个字符,小数点后面2位",
            trigger: "blur"
          }
        ],

        gcKs: [
          { required: true, message: "孔深不能为空", trigger: "blur" },
          {
            min: 1,
            max: 8,
            message: "长度在 1 到 8 个字符,小数点后面2位",
            trigger: "blur"
          }
        ],

        gcKj: [
          { required: true, message: "孔径不能为空", trigger: "blur" },
          {
            min: 1,
            max: 8,
            message: "长度在 1 到 8 个字符,小数点后面2位",
            trigger: "blur"
          }
        ],

        kkX: [
          { required: true, message: "孔口坐标X不能为空", trigger: "blur" }
        ],

        kkY: [
          { required: true, message: "孔口坐标Y不能为空", trigger: "blur" }
        ],

        kkZ: [
          { required: true, message: "孔口坐标Z不能为空", trigger: "blur" }
        ],

        dataTime: [
          { required: true, message: "数据定义时间不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    this.getList();
    listSensorTypeData(this.TypeParams).then(response => {
      this.sensorTypeList = response.rows;
    });
  },
  methods: {
    // 限制只能在输入框中输入小数点后两位
    inputnum() {
      this.form.gcScch = this.form.gcScch.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
      this.form.gcScms = this.form.gcScms.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
      this.form.gcKs = this.form.gcKs.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
      this.form.gcKj = this.form.gcKj.replace(
        /^(\-)*(\d+)\.(\d\d).*$/,
        "$1$2.$3"
      );
    },
    /** 查询长观孔测点基础信息列表 */
    getList() {
      this.loading = true;
      this.queryParams.params = {};
      if (null != this.daterangeDataTime && "" != this.daterangeDataTime) {
        this.queryParams.params["beginDataTime"] = this.daterangeDataTime[0];
        this.queryParams.params["endDataTime"] = this.daterangeDataTime[1];
      }
      listHoleDefine(this.queryParams).then(response => {
        this.holeDefineList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,

        mineCode: null,

        mineName: null,

        cdCode: null,

        sensorType: null,

        sensorInstallation: null,

        gcSccw: null,

        gcScch: null,

        gcScms: null,

        gcKs: null,

        gcKj: null,

        kkX: null,

        kkY: null,

        kkZ: null,

        dataTime: null,

        createBy: null,

        createTime: null,

        updateBy: null,

        updateTime: null,

        remark: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.daterangeDataTime = [];
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加长观孔测点基础信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids;
      getHoleDefine(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改长观孔测点基础信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateHoleDefine(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addHoleDefine(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm("是否确认删除当前选中数据？", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning"
      })
        .then(function() {
          return delHoleDefine(ids);
        })
        .then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
        .catch(() => {});
    }
  }
};
</script>
